﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.CacheData
{
    public class ControlDomain
    {
        /// <summary>
        /// 域编号
        /// </summary>
        public string Id { get; set; }

        public int Index { get; set; }

        /// <summary>
        /// 域名称
        /// </summary>
        public string Caption { get; set; }

        /// <summary>
        /// 方案名
        /// </summary>
        public string SolutionName { get; set; }

        /// <summary>
        /// 方案描述
        /// </summary>
        public string SolutionCaption { get; set; }

        public string CreatorName { get; set; }
        public string CreatorAccount { get; set; }
        public string CreatorIdentity { get; set; }

        private List<Client> _clientList;
        private object _clientListLockObj = new object();
        private int _clientCount = 0;

        private List<SceneState> _sceneStateList;
        private SceneState _currentSceneState;

        /// <summary>
        /// 操作者Identity，只有操作者才能发送信息给其他人员
        /// </summary>
        public string OperatorIdentity { get; private set; }

        private object _vsLockObj = new object();
        public long ViewStateAge { get; set; }
        public string ViewStateToken { get; set; }

        public string MainServerIP { get; private set; }
        public int MainServerPort { get; private set; }
        /// <summary>
        /// 是否在主服务上
        /// </summary>
        public bool IsInMainServer
        {
            get
            {
                if (!string.IsNullOrEmpty(CZEnvironment.Instance.ServerIP)
                    && string.Equals(CZEnvironment.Instance.ServerIP, MainServerIP)
                    && CZEnvironment.Instance.ServerPort == MainServerPort)
                {
                    return true;
                }
                return false;
            }
        }

        public ControlDomain()
        {
            _clientList = new List<Client>();
            _sceneStateList = new List<SceneState>();
        }

        /// <summary>
        /// 设置主服务器信息
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public void UpdateMainServer(string ip, int port)
        {
            this.MainServerIP = ip;
            this.MainServerPort = port;
        }

        #region Client list methods

        /// <summary>
        /// 是否含有任何的客户端，如果没有客户端改域将被删除
        /// </summary>
        /// <returns></returns>
        public bool HasAnyClient()
        {
            if (_clientCount > 0)
            {
                return true;
            }
            return false;
        }

        public List<Client> GetClientList()
        {
            return _clientList;
        }

        /// <summary>
        /// 添加客户端到域中
        /// </summary>
        /// <param name="client"></param>
        public void AddClient(Client client)
        {
            lock (_clientListLockObj)
            {
                _clientList.Add(client);
                _clientCount++;
            }
            client.IsInDomain = true;
            client.Domain = this;
        }

        /// <summary>
        /// 如果用户存在替换，如果不存在就添加
        /// </summary>
        /// <param name="client"></param>
        public void ExistReplaceNotExistAdd(Client client)
        {
            for (int index = 0; index < _clientList.Count; index++)
            {
                if(_clientList[index].UserIdentity == client.UserIdentity)
                {
                    _clientList[index] = client;
                    client.IsInDomain = true;
                    client.Domain = this;
                    return;
                }
            }

            AddClient(client);
        }

        public void RemoveClient(Client client)
        {
            lock (_clientListLockObj)
            {
                _clientList.Remove(client);
                _clientCount--;
            }
            client.IsInDomain = false;
            client.Domain = null;
        }

        public Client GetClient(string userIdentity)
        {
            for (int index = 0; index < _clientList.Count; index++)
            {
                Client client = _clientList[index];
                if (client.UserIdentity == userIdentity)
                {
                    return client;
                }
            }
            return null;
        }

        /// <summary>
        /// 更新客户列表里的此客户端对象,如果不存在就添加
        /// </summary>
        /// <param name="userIdentity"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public bool UpdateClient(string userIdentity, Client client)
        {
            bool isExist = false; ;
            for (int index = 0; index < _clientList.Count; index++)
            {
                if (_clientList[index].UserIdentity == userIdentity)
                {
                    _clientList[index] = client;
                    client.IsInDomain = true;
                    client.Domain = this;
                    isExist = true;
                    break;
                }
            }
            if (!isExist)
            {
                _clientList.Add(client);
                client.IsInDomain = true;
                client.Domain = this;
            }
            return true;
        }
        #endregion

        #region SceneState list operate methods

        public List<SceneState> GetSceneList()
        {
            return _sceneStateList;
        }
        /// <summary>
        /// 获取当前显示的场景
        /// </summary>
        /// <returns></returns>
        public SceneState GetCurrentScene()
        {
            return _currentSceneState;
        }

        public void SetCurrentScene(SceneState scene)
        {
            _currentSceneState = scene;
        }

        public SceneState GetScene(string name)
        {
            for (int index = 0; index < _sceneStateList.Count; index++)
            {
                if (_sceneStateList[index].Name == name)
                {
                    return _sceneStateList[index];
                }
            }
            return null;
        }

        public void AddScene(SceneState scene)
        {
            _sceneStateList.Add(scene);
        }

        /// <summary>
        /// 获取场景，如果不存在则添加
        /// </summary>
        /// <param name="sceneName"></param>
        /// <returns></returns>
        public SceneState GetOrAddScene(string sceneName)
        {
            SceneState scene = this.GetScene(sceneName);

            if (scene == null)
            {
                scene = new SceneState();
                scene.Name = sceneName;
                AddScene(scene);
            }
            return scene;
        }

        #endregion

        /// <summary>
        /// 验证操作员是否有效，操作员是否还在用户列表内
        /// </summary>
        /// <returns>true:有操作员，false:没有操作员</returns>
        public bool ValidateOperator()
        {
            if (string.IsNullOrEmpty(OperatorIdentity))
            {
                return false;
            }

            for (int index = 0; index < _clientList.Count; index++)
            {
                if (_clientList[index].UserIdentity == OperatorIdentity)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 设置操作员
        /// </summary>
        /// <param name="operatorClient"></param>
        public void SetOperator(Client operatorClient)
        {
            OperatorIdentity = operatorClient.UserIdentity;
        }

        /// <summary>
        /// 是否含有控制端
        /// </summary>
        /// <returns></returns>
        public bool HasAnyConsoleClient()
        {
            for (int index = 0; index < _clientList.Count; index++)
            {
                if (_clientList[index].AppType.ToUpper() == "CONSOLEAPP")
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获得控制端
        /// </summary>
        /// <returns></returns>
        public Client GetConsoleClient()
        {
            for (int index = 0; index < _clientList.Count; index++)
            {
                if (_clientList[index].AppType.ToUpper() == "CONSOLEAPP")
                {
                    return _clientList[index];
                }
            }
            return null;
        }

        #region viewState

        /// <summary>
        /// 更新视图状态
        /// </summary>
        /// <param name="solution"></param>
        /// <param name="newAge"></param>
        /// <param name="newToken"></param>
        public void UpdateViewState(string solution, long newAge, string newToken)
        {
            lock (_vsLockObj)
            {
                SolutionName = solution;
                ViewStateAge = newAge;
                ViewStateToken = newToken;
            }
        }

        public void ResetViewState()
        {
            lock (_vsLockObj)
            {
                SolutionName = string.Empty;
                ViewStateAge = 0;
                ViewStateToken = string.Empty;
            }
        }

        #endregion
    }
}
